package com.twu.biblioteca.content;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by mduemcke on 9/25/14.
 */
public class MovieContent extends ContentAbstract<Movie> {

    public MovieContent() {
        super();
    }

    public void checkoutItem(String title) {
        super.checkoutItem(title, "Thank you! Enjoy the movie", "That movie is not available.");
    }

    @Override
    protected void fillContent() {
        availableItems = new LinkedList<Movie>();
        availableItems.add(new Movie("The Truman Show", "Peter Weir", 1998, 9));
        availableItems.add(new Movie("Being John Malkovich", "Spike Jonze", 2007, 8));
        availableItems.add(new Movie("Boyhood", "Josh Boone", 2014, -1));
    }

    @Override
    protected String listToString(List<Movie> l) {
        String formatted = "%-50s|%-50s|%-10s|%-10s|\n";
        String listAsString = String.format(formatted, "Name", "Director", "Year", "Rating");

        for(Movie m : l) {
            String rating = Integer.toString(m.rating);
            if(rating.equals("-1")) {
                rating = "unrated";
            }
            listAsString += String.format(formatted, m.name, m.director, Integer.toString(m.year), rating);
        }
        return listAsString;
    }

    @Override
    protected int getIndexFromTitle(String title, List<Movie> list) {
        for (int i = 0; i < list.size(); i++) {
            Movie m = list.get(i);
            if (m.name.equals(title)) {
                return i;
            }
        }
        return -1;
    }

}

